package cn.com.pajx.pajx_visitor_v3.ui.view.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import java.util.Objects;

import cn.com.pajx.pajx_visitor_v3.R;
import cn.com.pajx.pajx_visitor_v3.utils.DialogUtil;

/**
 * create by ruancw on 2024/2/28.
 * description 通用的圆角Dialog
 */
@SuppressLint("NonConstantResourceId")
public class CommonDialog extends AlertDialog implements View.OnClickListener {

    private final Context mContext;

    private String title = "温馨提示", des, confirmText = "确定";

    public CommonDialog(@NonNull Context context) {
        super(context);
        mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.common_dialog);
        //设置背景透明，不然会出现白色直角问题
        initConfig();
        //设置View初始化参数
        initView();
        //初始化Dialog的参数（宽高等）
        initParams();
    }

    private void initConfig() {
        Window window = getWindow();
        if (window != null) {
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }
        setCanceledOnTouchOutside(false);
        DialogUtil.focusNotAle(getWindow());
        DialogUtil.hideNavigationBar(getWindow());//隐藏导航栏
        DialogUtil.clearFocusNotAle(getWindow());
    }

    /**
     * 初始化控件数据
     */
    @SuppressLint("UseCompatLoadingForDrawables")
    private void initView() {
        TextView tvTitle = findViewById(R.id.tv_title);
        TextView tvDes = findViewById(R.id.tv_des);
        ImageView ivClose = findViewById(R.id.iv_close);
        TextView tvConfirm = findViewById(R.id.tv_confirm);
        TextView tvCancel = findViewById(R.id.tv_cancel);

        tvTitle.setText(title);//标题
        tvDes.setText(des);
        tvConfirm.setText(confirmText);
        tvConfirm.setOnClickListener(this);
        tvCancel.setOnClickListener(this);
        ivClose.setOnClickListener(this);

    }

    /**
     * 初始化Dialog的参数
     */
    private void initParams() {
        //设置参数必须在show之后，不然没有效果
        WindowManager m = Objects.requireNonNull(getWindow()).getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高度
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = (int) (d.getWidth() * 0.4); // 宽度设置为屏幕的0.8，根据实际情况调整
        getWindow().setAttributes(params);
        //解决不显示输入法的问题
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    }


    /**
     * 设置标题栏文本
     *
     * @param title 标题
     */
    public CommonDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    /**
     * 设置描述信息
     *
     * @param des 描述信息
     */
    public CommonDialog setMessage(String des) {
        this.des = des;
        return this;
    }

    private ChildClickListener childClickListener;

    /**
     * 控件点击事件
     *
     * @param childClickListener childClickListener
     */
    public CommonDialog setChildClickListener(ChildClickListener childClickListener) {
        this.childClickListener = childClickListener;
        return this;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
            case R.id.iv_close:
                dismiss();
                break;
            case R.id.tv_confirm:
                childClickListener.onConfirmListener(CommonDialog.this);
                break;
        }
    }

    public CommonDialog setConfirmText(String confirmText) {
        this.confirmText = confirmText;
        return this;
    }

    /**
     * 确定按钮点击的监听接口
     */
    public interface ChildClickListener {
        void onConfirmListener(CommonDialog commonDialog);
    }
}
